package Gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.WindowEvent;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.IntervalMarker;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.ClusteredXYBarRenderer;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.Layer;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;
/**
 * this class this class represent the Chartmake interface and build histogram
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class Chartmake extends ApplicationFrame {

    /**
     * Creates a new demo instance.
     *
     * @param title  the frame title.
     */
    public Chartmake(String title,Map<Double, Double> map,Double interval) {
        super(title);
        IntervalXYDataset dataset = createDataset(map,interval);
        JFreeChart chart = createChart(dataset,title);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(400, 270));
       
       
        setContentPane(chartPanel);
    }
    public void windowClosing(final WindowEvent evt)
    {
    	if(evt.getWindow() == this){
    	dispose();
    	}
    }
    
    /**
     * Creates a sample dataset.
     * 
     * @return A sample dataset.
     */
    private IntervalXYDataset createDataset(Map<Double, Double> map,Double interval) {
        final XYSeries series = new XYSeries("Values");
           
       
        for (Map.Entry<Double, Double> entry : map.entrySet()) {
            Double key = entry.getKey();
            Double value = entry.getValue();
            series.add(key,value);
            
        }
      
        final XYSeriesCollection dataset = new XYSeriesCollection(series);
        //dataset.setAutoWidth(false);
        dataset.setIntervalWidth(interval);
        return dataset;
    }

    /**
     * Creates a sample chart.
     * 
     * @param dataset  the dataset.
     * 
     * @return A sample chart.
     */
    private JFreeChart createChart(IntervalXYDataset dataset,String s) {
        final JFreeChart chart = ChartFactory.createXYBarChart(
            s,
            "Values", 
            false,
            "%", 
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );
        XYBarRenderer renderer = new ClusteredXYBarRenderer();
        renderer.setBarAlignmentFactor(0.5);
       XYPlot plot = (XYPlot) chart.getPlot();
       
        final IntervalMarker target = new IntervalMarker(400.0, 700.0);
        
        target.setLabel("Target Range");
        target.setLabelFont(new Font("SansSerif", Font.ITALIC, 11));
        target.setLabelAnchor(RectangleAnchor.RIGHT);
        target.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
        target.setPaint(new Color(222, 222, 255, 128));
        plot.addRangeMarker(target, Layer.BACKGROUND);
        return chart;    
    }
    
    
    
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    

}